package com.cxs.distributeddemo.util;

import ohos.app.Context;
import ohos.data.distributed.common.*;
import ohos.data.distributed.user.SingleKvStore;

public class KvStoreUtils {
    private static KvManager kvManager;
    private static SingleKvStore kvStore;
    static String storeId = "distributedApp";

    public static SingleKvStore getKvStore(Context context) {
        KvManagerConfig kvManagerConfig = new KvManagerConfig(context);
        kvManager = KvManagerFactory.getInstance().createKvManager(kvManagerConfig);

        Options options = new Options();
        options.setCreateIfMissing(true)
                .setEncrypt(false)
                .setKvStoreType(KvStoreType.SINGLE_VERSION);


        kvStore = kvManager.getKvStore(options, storeId);

        return kvStore;
    }

    public static void closeDB(SingleKvStore kvStore) {
        try {
            kvManager.closeKvStore(kvStore);
        }catch (KvStoreException e) {
            e.printStackTrace();
        }
    }

    public static void delDB() {
        try {
            kvManager.deleteKvStore(storeId);
        }catch (KvStoreException e) {
            e.printStackTrace();
        }
    }
}
